{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Install\n",
    "vnpy_fxdayu: https://github.com/xingetouzi/vnpy_fxdayu\n",
    "\n",
    "Mongodb: https://www.mongodb.com/download-center#community\n",
    "\n",
    "安装说明: https://github.com/xingetouzi/vnpy_fxdayu/wiki/Windows环境安装\n",
    "\n",
    "建议安装robomongo作为可视化数据库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 根据csv数据格式入数据\n",
    "1. pymongo\n",
    "2. VT_setting.json\n",
    "\n",
    "数据下载地址： https://pan.baidu.com/s/1BiIjSX2dCYPy03ZcC7NR5A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import csv\n",
    "from datetime import datetime, timedelta\n",
    "from time import time\n",
    "import pymongo\n",
    "from vnpy.trader.vtGlobal import globalSetting\n",
    "from vnpy.trader.vtConstant import *\n",
    "from vnpy.trader.vtObject import VtBarData\n",
    "\n",
    "def loadCoinCsv(fileName, dbName, symbol):\n",
    "    \"\"\"将数字货币数据导出的csv格式的历史分钟数据插入到Mongo数据库中\"\"\"\n",
    "    start = time()\n",
    "    print('开始读取CSV文件%s中的数据插入到%s的%s中' %(fileName, dbName, symbol))\n",
    "\n",
    "    # 锁定集合，并创建索引\n",
    "    client = pymongo.MongoClient(globalSetting['mongoHost'], globalSetting['mongoPort'])\n",
    "    collection = client[dbName][symbol]\n",
    "    collection.ensure_index([('datetime', pymongo.ASCENDING)], unique=True)\n",
    "\n",
    "    # 读取数据和插入到数据库\n",
    "    reader = csv.reader(open(fileName,\"r\"))\n",
    "\n",
    "    for d in reader:\n",
    "        if len(d[-1]) == 16:\n",
    "            bar = VtBarData()\n",
    "            bar.vtSymbol = d[-1]\n",
    "            bar.symbol, bar.exchange = bar.vtSymbol.split(':')\n",
    "            bar.datetime = datetime.strptime(d[3], '%Y-%m-%d %H:%M:%S')\n",
    "            bar.date = bar.datetime.date().strftime('%Y%m%d')\n",
    "            bar.time = bar.datetime.time().strftime('%H:%M:%S')\n",
    "            if bar.datetime:\n",
    "                bar.high = float(d[6])\n",
    "                bar.low = float(d[7])\n",
    "                bar.open = float(d[8])\n",
    "                bar.close = float(d[1])\n",
    "                bar.volume = float(d[13])\n",
    "\n",
    "            flt = {'datetime': bar.datetime}\n",
    "            collection.update_one(flt, {'$set':bar.__dict__}, upsert=True)\n",
    "            print('%s \\t %s' % (bar.date, bar.time))\n",
    "\n",
    "    print('插入完毕，耗时：%s' % (time()-start))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "loadCoinCsv('tBTCUSD.csv', \"VnTrader_1Min_Db\", 'tBCHUSD:bitfinex')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 安装datautils可以管理Mongodb的数据\n",
    "github地址:  https://github.com/cheatm/datautils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       close      date            datetime  exchange gatewayName    high  \\\n",
      "0     696.57  20180629 2018-06-29 00:00:00  bitfinex              696.57   \n",
      "1     696.29  20180629 2018-06-29 00:01:00  bitfinex              696.29   \n",
      "2     695.70  20180629 2018-06-29 00:02:00  bitfinex              695.70   \n",
      "3     696.22  20180629 2018-06-29 00:05:00  bitfinex              696.22   \n",
      "4     696.26  20180629 2018-06-29 00:06:00  bitfinex              696.26   \n",
      "5     696.25  20180629 2018-06-29 00:07:00  bitfinex              696.26   \n",
      "6     696.26  20180629 2018-06-29 00:09:00  bitfinex              696.26   \n",
      "7     696.91  20180629 2018-06-29 00:11:00  bitfinex              696.91   \n",
      "8     697.47  20180629 2018-06-29 00:12:00  bitfinex              697.47   \n",
      "9     697.46  20180629 2018-06-29 00:13:00  bitfinex              697.46   \n",
      "10    697.41  20180629 2018-06-29 00:14:00  bitfinex              697.41   \n",
      "11    697.40  20180629 2018-06-29 00:15:00  bitfinex              697.42   \n",
      "12    697.37  20180629 2018-06-29 00:16:00  bitfinex              697.37   \n",
      "13    697.34  20180629 2018-06-29 00:17:00  bitfinex              697.34   \n",
      "14    697.38  20180629 2018-06-29 00:18:00  bitfinex              697.42   \n",
      "15    697.42  20180629 2018-06-29 00:19:00  bitfinex              697.42   \n",
      "16    697.42  20180629 2018-06-29 00:20:00  bitfinex              697.42   \n",
      "17    697.42  20180629 2018-06-29 00:25:00  bitfinex              697.42   \n",
      "18    697.80  20180629 2018-06-29 00:27:00  bitfinex              697.80   \n",
      "19    697.79  20180629 2018-06-29 00:28:00  bitfinex              697.80   \n",
      "20    697.80  20180629 2018-06-29 00:29:00  bitfinex              697.80   \n",
      "21    697.79  20180629 2018-06-29 00:30:00  bitfinex              697.80   \n",
      "22    697.79  20180629 2018-06-29 00:32:00  bitfinex              697.79   \n",
      "23    697.66  20180629 2018-06-29 00:33:00  bitfinex              697.79   \n",
      "24    697.04  20180629 2018-06-29 00:34:00  bitfinex              697.53   \n",
      "25    697.00  20180629 2018-06-29 00:35:00  bitfinex              697.06   \n",
      "26    695.96  20180629 2018-06-29 00:36:00  bitfinex              697.00   \n",
      "27    695.61  20180629 2018-06-29 00:37:00  bitfinex              695.68   \n",
      "28    696.91  20180629 2018-06-29 00:38:00  bitfinex              696.91   \n",
      "29    696.50  20180629 2018-06-29 00:39:00  bitfinex              696.50   \n",
      "...      ...       ...                 ...       ...         ...     ...   \n",
      "1144  661.76  20180629 2018-06-29 23:20:00  bitfinex              661.76   \n",
      "1145  661.21  20180629 2018-06-29 23:21:00  bitfinex              661.21   \n",
      "1146  661.21  20180629 2018-06-29 23:22:00  bitfinex              661.21   \n",
      "1147  661.21  20180629 2018-06-29 23:25:00  bitfinex              661.25   \n",
      "1148  661.53  20180629 2018-06-29 23:27:00  bitfinex              661.53   \n",
      "1149  661.69  20180629 2018-06-29 23:28:00  bitfinex              661.69   \n",
      "1150  661.90  20180629 2018-06-29 23:29:00  bitfinex              661.90   \n",
      "1151  662.00  20180629 2018-06-29 23:30:00  bitfinex              662.00   \n",
      "1152  661.79  20180629 2018-06-29 23:31:00  bitfinex              662.27   \n",
      "1153  661.60  20180629 2018-06-29 23:32:00  bitfinex              661.67   \n",
      "1154  661.50  20180629 2018-06-29 23:33:00  bitfinex              661.55   \n",
      "1155  661.46  20180629 2018-06-29 23:34:00  bitfinex              661.47   \n",
      "1156  661.50  20180629 2018-06-29 23:35:00  bitfinex              661.50   \n",
      "1157  661.40  20180629 2018-06-29 23:36:00  bitfinex              661.40   \n",
      "1158  661.38  20180629 2018-06-29 23:37:00  bitfinex              661.38   \n",
      "1159  661.40  20180629 2018-06-29 23:38:00  bitfinex              661.40   \n",
      "1160  661.50  20180629 2018-06-29 23:39:00  bitfinex              661.50   \n",
      "1161  661.78  20180629 2018-06-29 23:40:00  bitfinex              661.78   \n",
      "1162  661.89  20180629 2018-06-29 23:41:00  bitfinex              662.00   \n",
      "1163  661.77  20180629 2018-06-29 23:42:00  bitfinex              661.77   \n",
      "1164  660.46  20180629 2018-06-29 23:44:00  bitfinex              660.99   \n",
      "1165  660.00  20180629 2018-06-29 23:45:00  bitfinex              661.49   \n",
      "1166  659.80  20180629 2018-06-29 23:46:00  bitfinex              660.00   \n",
      "1167  660.00  20180629 2018-06-29 23:47:00  bitfinex              660.00   \n",
      "1168  659.18  20180629 2018-06-29 23:53:00  bitfinex              659.18   \n",
      "1169  659.96  20180629 2018-06-29 23:55:00  bitfinex              659.96   \n",
      "1170  659.93  20180629 2018-06-29 23:56:00  bitfinex              659.93   \n",
      "1171  660.50  20180629 2018-06-29 23:57:00  bitfinex              660.50   \n",
      "1172  663.99  20180629 2018-06-29 23:58:00  bitfinex              663.99   \n",
      "1173  663.80  20180629 2018-06-29 23:59:00  bitfinex              663.80   \n",
      "\n",
      "         low    open  openInterest rawData   symbol             time  \\\n",
      "0     696.54  696.54             0    None  tBCHUSD  00:00:00.000000   \n",
      "1     695.40  696.29             0    None  tBCHUSD  00:01:00.000000   \n",
      "2     695.70  695.70             0    None  tBCHUSD  00:02:00.000000   \n",
      "3     696.22  696.22             0    None  tBCHUSD  00:05:00.000000   \n",
      "4     696.26  696.26             0    None  tBCHUSD  00:06:00.000000   \n",
      "5     696.20  696.20             0    None  tBCHUSD  00:07:00.000000   \n",
      "6     696.26  696.26             0    None  tBCHUSD  00:09:00.000000   \n",
      "7     696.26  696.26             0    None  tBCHUSD  00:11:00.000000   \n",
      "8     696.91  696.91             0    None  tBCHUSD  00:12:00.000000   \n",
      "9     697.46  697.46             0    None  tBCHUSD  00:13:00.000000   \n",
      "10    697.38  697.38             0    None  tBCHUSD  00:14:00.000000   \n",
      "11    697.40  697.41             0    None  tBCHUSD  00:15:00.000000   \n",
      "12    697.37  697.37             0    None  tBCHUSD  00:16:00.000000   \n",
      "13    697.34  697.34             0    None  tBCHUSD  00:17:00.000000   \n",
      "14    697.38  697.41             0    None  tBCHUSD  00:18:00.000000   \n",
      "15    697.42  697.42             0    None  tBCHUSD  00:19:00.000000   \n",
      "16    697.42  697.42             0    None  tBCHUSD  00:20:00.000000   \n",
      "17    697.42  697.42             0    None  tBCHUSD  00:25:00.000000   \n",
      "18    697.42  697.42             0    None  tBCHUSD  00:27:00.000000   \n",
      "19    697.79  697.80             0    None  tBCHUSD  00:28:00.000000   \n",
      "20    697.80  697.80             0    None  tBCHUSD  00:29:00.000000   \n",
      "21    697.79  697.79             0    None  tBCHUSD  00:30:00.000000   \n",
      "22    697.79  697.79             0    None  tBCHUSD  00:32:00.000000   \n",
      "23    697.66  697.79             0    None  tBCHUSD  00:33:00.000000   \n",
      "24    697.01  697.53             0    None  tBCHUSD  00:34:00.000000   \n",
      "25    697.00  697.06             0    None  tBCHUSD  00:35:00.000000   \n",
      "26    695.96  697.00             0    None  tBCHUSD  00:36:00.000000   \n",
      "27    695.56  695.68             0    None  tBCHUSD  00:37:00.000000   \n",
      "28    695.47  695.47             0    None  tBCHUSD  00:38:00.000000   \n",
      "29    696.23  696.38             0    None  tBCHUSD  00:39:00.000000   \n",
      "...      ...     ...           ...     ...      ...              ...   \n",
      "1144  661.27  661.27             0    None  tBCHUSD  23:20:00.000000   \n",
      "1145  661.21  661.21             0    None  tBCHUSD  23:21:00.000000   \n",
      "1146  661.21  661.21             0    None  tBCHUSD  23:22:00.000000   \n",
      "1147  661.21  661.25             0    None  tBCHUSD  23:25:00.000000   \n",
      "1148  661.53  661.53             0    None  tBCHUSD  23:27:00.000000   \n",
      "1149  661.54  661.54             0    None  tBCHUSD  23:28:00.000000   \n",
      "1150  661.54  661.76             0    None  tBCHUSD  23:29:00.000000   \n",
      "1151  661.66  661.98             0    None  tBCHUSD  23:30:00.000000   \n",
      "1152  661.69  661.99             0    None  tBCHUSD  23:31:00.000000   \n",
      "1153  661.55  661.67             0    None  tBCHUSD  23:32:00.000000   \n",
      "1154  661.47  661.55             0    None  tBCHUSD  23:33:00.000000   \n",
      "1155  661.46  661.46             0    None  tBCHUSD  23:34:00.000000   \n",
      "1156  661.39  661.40             0    None  tBCHUSD  23:35:00.000000   \n",
      "1157  661.37  661.37             0    None  tBCHUSD  23:36:00.000000   \n",
      "1158  661.38  661.38             0    None  tBCHUSD  23:37:00.000000   \n",
      "1159  661.37  661.37             0    None  tBCHUSD  23:38:00.000000   \n",
      "1160  661.27  661.27             0    None  tBCHUSD  23:39:00.000000   \n",
      "1161  661.77  661.77             0    None  tBCHUSD  23:40:00.000000   \n",
      "1162  661.80  661.80             0    None  tBCHUSD  23:41:00.000000   \n",
      "1163  661.77  661.77             0    None  tBCHUSD  23:42:00.000000   \n",
      "1164  660.26  660.99             0    None  tBCHUSD  23:44:00.000000   \n",
      "1165  659.99  660.58             0    None  tBCHUSD  23:45:00.000000   \n",
      "1166  659.78  659.78             0    None  tBCHUSD  23:46:00.000000   \n",
      "1167  659.90  659.90             0    None  tBCHUSD  23:47:00.000000   \n",
      "1168  659.18  659.18             0    None  tBCHUSD  23:53:00.000000   \n",
      "1169  659.96  659.96             0    None  tBCHUSD  23:55:00.000000   \n",
      "1170  659.93  659.93             0    None  tBCHUSD  23:56:00.000000   \n",
      "1171  659.94  659.94             0    None  tBCHUSD  23:57:00.000000   \n",
      "1172  662.00  662.00             0    None  tBCHUSD  23:58:00.000000   \n",
      "1173  663.44  663.44             0    None  tBCHUSD  23:59:00.000000   \n",
      "\n",
      "          volume          vtSymbol  \n",
      "0       0.242928  tBCHUSD:bitfinex  \n",
      "1       7.220000  tBCHUSD:bitfinex  \n",
      "2       5.740000  tBCHUSD:bitfinex  \n",
      "3       0.045947  tBCHUSD:bitfinex  \n",
      "4       0.337250  tBCHUSD:bitfinex  \n",
      "5       0.859260  tBCHUSD:bitfinex  \n",
      "6       0.168630  tBCHUSD:bitfinex  \n",
      "7       8.420088  tBCHUSD:bitfinex  \n",
      "8       2.000050  tBCHUSD:bitfinex  \n",
      "9       1.000000  tBCHUSD:bitfinex  \n",
      "10      1.200000  tBCHUSD:bitfinex  \n",
      "11      5.307516  tBCHUSD:bitfinex  \n",
      "12      0.100000  tBCHUSD:bitfinex  \n",
      "13      0.337250  tBCHUSD:bitfinex  \n",
      "14      4.522844  tBCHUSD:bitfinex  \n",
      "15      0.175000  tBCHUSD:bitfinex  \n",
      "16      2.444762  tBCHUSD:bitfinex  \n",
      "17      0.129100  tBCHUSD:bitfinex  \n",
      "18     18.189110  tBCHUSD:bitfinex  \n",
      "19      0.773190  tBCHUSD:bitfinex  \n",
      "20      0.800000  tBCHUSD:bitfinex  \n",
      "21     28.343147  tBCHUSD:bitfinex  \n",
      "22      0.286855  tBCHUSD:bitfinex  \n",
      "23      3.198690  tBCHUSD:bitfinex  \n",
      "24      3.400300  tBCHUSD:bitfinex  \n",
      "25      6.312870  tBCHUSD:bitfinex  \n",
      "26     16.303262  tBCHUSD:bitfinex  \n",
      "27      0.955140  tBCHUSD:bitfinex  \n",
      "28      6.082557  tBCHUSD:bitfinex  \n",
      "29      4.248550  tBCHUSD:bitfinex  \n",
      "...          ...               ...  \n",
      "1144    5.138737  tBCHUSD:bitfinex  \n",
      "1145    1.436044  tBCHUSD:bitfinex  \n",
      "1146    1.771498  tBCHUSD:bitfinex  \n",
      "1147    5.800000  tBCHUSD:bitfinex  \n",
      "1148    4.708696  tBCHUSD:bitfinex  \n",
      "1149    6.570728  tBCHUSD:bitfinex  \n",
      "1150   11.258261  tBCHUSD:bitfinex  \n",
      "1151   10.359476  tBCHUSD:bitfinex  \n",
      "1152    4.654592  tBCHUSD:bitfinex  \n",
      "1153    4.058822  tBCHUSD:bitfinex  \n",
      "1154    3.077035  tBCHUSD:bitfinex  \n",
      "1155    2.754114  tBCHUSD:bitfinex  \n",
      "1156    3.527877  tBCHUSD:bitfinex  \n",
      "1157    1.895549  tBCHUSD:bitfinex  \n",
      "1158    2.387415  tBCHUSD:bitfinex  \n",
      "1159    1.351178  tBCHUSD:bitfinex  \n",
      "1160    2.724035  tBCHUSD:bitfinex  \n",
      "1161    1.456741  tBCHUSD:bitfinex  \n",
      "1162    1.833992  tBCHUSD:bitfinex  \n",
      "1163    0.693808  tBCHUSD:bitfinex  \n",
      "1164   17.589597  tBCHUSD:bitfinex  \n",
      "1165  135.254587  tBCHUSD:bitfinex  \n",
      "1166   27.117812  tBCHUSD:bitfinex  \n",
      "1167   82.689991  tBCHUSD:bitfinex  \n",
      "1168    2.000000  tBCHUSD:bitfinex  \n",
      "1169    1.610853  tBCHUSD:bitfinex  \n",
      "1170    0.036000  tBCHUSD:bitfinex  \n",
      "1171   26.215686  tBCHUSD:bitfinex  \n",
      "1172   23.034906  tBCHUSD:bitfinex  \n",
      "1173    7.862831  tBCHUSD:bitfinex  \n",
      "\n",
      "[1174 rows x 14 columns]\n"
     ]
    }
   ],
   "source": [
    "from datautils.mongodb import read\n",
    "from pymongo import MongoClient\n",
    "from datetime import datetime\n",
    "\n",
    "client = MongoClient(\"192.168.0.104\", 37017)\n",
    "data = read(client[\"VnTrader_1Min_Db\"][\"tBCHUSD:bitfinex\"], datetime=(datetime(2018, 6, 29 ),datetime(2018, 6, 30 )))\n",
    "print(data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
